package rollercoaster.environment.utils;


import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import rollercoaster.commons.Material;
import rollercoaster.commons.Model;
import rollercoaster.environment.interfaces.IModelReader;
import rollercoaster.environment.interfaces.IObjFileReader;
import rollercoaster.environment.interfaces.IScenarioReader;
import rollercoaster.environment.utils.ModelMaker;
import rollercoaster.environment.utils.OBJFileModelReader;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ScenarioReader implements IScenarioReader{

	private String scenarioName;
	private HashMap<String, String> modelsList;
	private HashMap<String, HashMap<String, String>> modelsMaterials;
	private HashMap<String, String> materials;


	public ScenarioReader(String scenarioName) {
		super();
		this.scenarioName = scenarioName;
		getScenario(scenarioName);
	}



	
	public void getScenario(String scenarioName) {
		try {
			 
			File fXmlFile = new File("scenarios.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
		 	
			doc.getDocumentElement().normalize();


			loadModelsFromXML(doc);

		    } catch (Exception e) {
			e.printStackTrace();
		    }	  
	}

	
	private void loadModelsFromXML(Document doc){
		
		modelsMaterials=new HashMap<String, HashMap<String, String>>();
		modelsList=new HashMap<String, String>();
		materials = new HashMap<String, String>();

		//modelMaker=new ModelMaker(r);
		//defaultTexture="materials/default.jpg";

		NodeList nListScenario = doc.getElementsByTagName("scenario");
		int dim=nListScenario.getLength();
		int temp =0;
		
		Node nNodeScenario = nListScenario.item(temp);
		Element scenarioElement = (Element) nNodeScenario;
		
		while((temp<dim) && !(scenarioElement.getAttribute("id").equals(scenarioName))) {
			
			temp++;
			nNodeScenario = nListScenario.item(temp);
			scenarioElement = (Element) nNodeScenario;
			
		}
			if (temp<dim) {
				
				NodeList nListModel = nNodeScenario.getChildNodes();
				
				for (int i = 0; i < nListModel.getLength(); i++) {
					
					Node nNode = nListModel.item(i);
					
					if (nNode.getNodeType() == Node.ELEMENT_NODE) {
						 
						Element eElement = (Element) nNode;
						
						//objReader.setFilepath(eElement.getElementsByTagName("path").item(0).getTextContent());
						//System.out.println(eElement.getElementsByTagName("path").item(0).getTextContent());
						modelsList.put(eElement.getElementsByTagName("name").item(0).getTextContent(), eElement.getElementsByTagName("path").item(0).getTextContent());
						//Model newModel = modelMaker.makeModel();
						
						//ModelContainer newModelContainer = new ModelContainer();
						
						NodeList nListMaterials = eElement.getElementsByTagName("material");
						 
							
						 if (nListMaterials.getLength()>0){
							 for (int j = 0; j < nListMaterials.getLength(); j++) {
								 
								 Node nMaterial = nListMaterials.item(j);
								 
								 if (nMaterial.getNodeType() == Node.ELEMENT_NODE) {
									 
									 Element eMaterial = (Element) nMaterial;
									 
									 materials.put(eMaterial.getElementsByTagName("name").item(0).getTextContent(), eMaterial.getElementsByTagName("path").item(0).getTextContent());
							     }
								  
					          }
						 }
						 
						/*Map<String, ArrayList<ObjFace>> map = r.getListModels();
							
						for (Map.Entry<String, ArrayList<ObjFace>> entry : map.entrySet()) {
							
							r.setFaces(entry.getValue());
							Model newModel = modelMaker.makeModel();
							if (materials.get(entry.getKey())!=null){
								Material mat=new Material(materials.get(entry.getKey()));
								newModel.setMaterial(mat);
							}
							else {
								Material mat=new Material(defaultTexture);
								newModel.setMaterial(mat);
							}
							newModelContainer.add(newModel);
						}*/
							 
						
						modelsMaterials.put(eElement.getElementsByTagName("name").item(0).getTextContent(), materials);
							
						
					}
					
				}
			}
			else 
				System.out.println("Scenario not found!!");
			
		
		
	}

	
	public HashMap<String, HashMap<String, String>> getModelsMaterials() {
		// TODO Auto-generated method stub
		return modelsMaterials;
	}



	public HashMap<String, String> getModelsList() {
		// TODO Auto-generated method stub
		return modelsList;
	}



	
	
}
